package top.greatxiaozou.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.greatxiaozou.model.Result;
import top.greatxiaozou.service.AdminService;
import top.greatxiaozou.utils.ResultsUtils;
import top.greatxiaozou.utils.TokenUtils;

import java.util.HashMap;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

//    @PostMapping("/regist")
//    public ResponseEntity<Result> regist(){
//
//    }

    @GetMapping("/login")
    public ResponseEntity<Result> login(String adminName,String password){
        String token = TokenUtils.getToken(adminName, password);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken shiroToken = new UsernamePasswordToken(adminName,password);

        try {
            subject.login(shiroToken);
            return ResultsUtils.success(map,"尊贵的管理员"+adminName+",zys女装欢迎您！",HttpStatus.OK);
        }catch (UnknownAccountException e1){
            return ResultsUtils.faild("管理员不存在", HttpStatus.FORBIDDEN);
        }catch (IncorrectCredentialsException e2){
            return ResultsUtils.faild("密码错误",HttpStatus.FORBIDDEN);
        }


    }
}
